package com.newnius.picbrowser;
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import static java.lang.Integer.min;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.TransferHandler;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Newnius
 */

public class detail extends javax.swing.JFrame {

    /**
     * Creates new form detail
     *
     * @param url 图片的本地地址
     */
    public detail(String url) {
        initComponents();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        display(url);
        try{
        jScrollPane1.setTransferHandler(
                new TransferHandler() {
                    @Override
                    public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
                        return true;
                    }
                    @Override
                    public boolean importData(JComponent comp, Transferable t) {
                        try {
                            if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                                List files;
                                files = (List) t
                                .getTransferData(DataFlavor.javaFileListFlavor);
                                for (Object file : files) {
                                    File f = (File) file;
                                    display(f.getAbsolutePath());
                                }
                                return true;
                            }
                        } catch (UnsupportedFlavorException e) {
                            System.out.println("wrong at line:58");
                        } catch (IOException e) {
                            System.out.println("wrong at line:60");
                        }
                        return super.importData(comp, t);
                    }
                }
        );
        }catch(Exception e){
            System.out.println("wrong at line:67");
        }
    }

    private void display(String location) {
        try {
            if(!(location.endsWith(".jpg")||location.endsWith(".gif")||location.endsWith(".png"))){
                jLabel3.setText("这是图片？我学的少，你别骗我。");
                location="img/null.gif";
            }
            File picture = new File(location);
            BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));
            int width = sourceImg.getWidth();
            int height = sourceImg.getHeight();
            jLabel1.setText("");
            String info = "";
            info = info + "像素大小:(" + width + "*" + height + ")                          ";
            info = info + "本地位置：" + picture.getAbsolutePath() + "                         ";
            info = info + "图片大小：" + String.format("%.1f", picture.length() / 1024.0) + "KB";
            jLabel2.setText(info);

            if (width > 800 || height > 500) {
                width = min(width, 800);
                height = min(height, 500);
                ImageIcon p = new ImageIcon(location);
                Image pt = p.getImage().getScaledInstance(width, height, 0);
                jLabel1.setIcon(new ImageIcon(pt));

            } else {
                jLabel1.setIcon(new ImageIcon(location));
            }
            setSize(width + 50, height + 100);//窗口大小  

        } catch (Exception e) {
            System.out.println("wrong at line:101");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("查看大图");

        jLabel1.setText("图片加载失败");
        jLabel1.setToolTipText("双击关闭窗体");
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jLabel1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jLabel2.setText("暂无图片信息");
        getContentPane().add(jLabel2, java.awt.BorderLayout.PAGE_END);

        jLabel3.setText("查看大图啦");
        getContentPane().add(jLabel3, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
        // TODO add your handling code here:
        if(evt.getClickCount()==2){
            dispose();
        }
    }//GEN-LAST:event_jLabel1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
